
#include <document_header.h>
#include <gdk/gdk.h>
#include "utils.h"




struct _DocumentHeaderPrivate {
	GtkButton* close_btn;
	GtkLabel* title_lbl;
	Document* doc;
};

#define DOCUMENT_HEADER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_DOCUMENT_HEADER, DocumentHeaderPrivate))
enum  {
	DOCUMENT_HEADER_DUMMY_PROPERTY,
	DOCUMENT_HEADER_TEXT
};
static gint document_header_count = 0;
static void _document_header_on_close_btn_clicked_gtk_button_clicked (GtkButton* _sender, gpointer self);
static GdkColor* _gdk_color_dup (GdkColor* self);
static GObject * document_header_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static gpointer document_header_parent_class = NULL;
static void document_header_finalize (GObject* obj);



static void _document_header_on_close_btn_clicked_gtk_button_clicked (GtkButton* _sender, gpointer self) {
	document_header_on_close_btn_clicked (self);
}


DocumentHeader* document_header_construct (GType object_type, const char* filename, Document** doc) {
	DocumentHeader * self;
	g_return_val_if_fail (doc != NULL, NULL);
	self = g_object_newv (object_type, 0, NULL);
	self->priv->doc = (*doc);
	g_signal_connect_object (self->priv->close_btn, "clicked", (GCallback) _document_header_on_close_btn_clicked_gtk_button_clicked, self, 0);
	if (filename == NULL) {
		char* _tmp1;
		char* _tmp0;
		document_header_count = document_header_count + 1;
		_tmp1 = NULL;
		_tmp0 = NULL;
		document_header_set_text (self, _tmp1 = g_strconcat ("Nowy plik ", _tmp0 = g_strdup_printf ("%i", document_header_count), NULL));
		_tmp1 = (g_free (_tmp1), NULL);
		_tmp0 = (g_free (_tmp0), NULL);
	} else {
		char* _tmp2;
		_tmp2 = NULL;
		document_header_set_text (self, _tmp2 = g_path_get_basename (filename));
		_tmp2 = (g_free (_tmp2), NULL);
	}
	return self;
}


DocumentHeader* document_header_new (const char* filename, Document** doc) {
	return document_header_construct (TYPE_DOCUMENT_HEADER, filename, doc);
}


static GdkColor* _gdk_color_dup (GdkColor* self) {
	GdkColor* dup;
	dup = g_new0 (GdkColor, 1);
	memcpy (dup, self, sizeof (GdkColor));
	return dup;
}


void document_header_set_state (DocumentHeader* self, DocumentDocumentState state) {
	GdkColor* c;
	g_return_if_fail (self != NULL);
	c = NULL;
	switch (state) {
		case DOCUMENT_DOCUMENT_STATE_NO_MODIFIED:
		{
			{
				GdkColor* _tmp3 = {0};
				GdkColor* _tmp2 = {0};
				gboolean _tmp1;
				GdkColor _tmp0 = {0};
				_tmp1 = gdk_color_parse ("#000000", &_tmp0);
				c = (_tmp2 = (_tmp3 = &_tmp0, (_tmp3 == NULL) ? NULL : _gdk_color_dup (_tmp3)), (c == NULL) ? NULL : (c = (g_free (c), NULL)), _tmp2);
				_tmp1;
			}
			break;
		}
		case DOCUMENT_DOCUMENT_STATE_MODIFIED:
		{
			{
				GdkColor* _tmp7 = {0};
				GdkColor* _tmp6 = {0};
				gboolean _tmp5;
				GdkColor _tmp4 = {0};
				_tmp5 = gdk_color_parse ("#dd0000", &_tmp4);
				c = (_tmp6 = (_tmp7 = &_tmp4, (_tmp7 == NULL) ? NULL : _gdk_color_dup (_tmp7)), (c == NULL) ? NULL : (c = (g_free (c), NULL)), _tmp6);
				_tmp5;
			}
			break;
		}
		case DOCUMENT_DOCUMENT_STATE_READ_ONLY:
		{
			{
				GdkColor* _tmp11 = {0};
				GdkColor* _tmp10 = {0};
				gboolean _tmp9;
				GdkColor _tmp8 = {0};
				_tmp9 = gdk_color_parse ("silver", &_tmp8);
				c = (_tmp10 = (_tmp11 = &_tmp8, (_tmp11 == NULL) ? NULL : _gdk_color_dup (_tmp11)), (c == NULL) ? NULL : (c = (g_free (c), NULL)), _tmp10);
				_tmp9;
			}
			break;
		}
	}
	gtk_widget_modify_fg ((GtkWidget*) self->priv->title_lbl, GTK_STATE_ACTIVE, c);
	gtk_widget_modify_fg ((GtkWidget*) self->priv->title_lbl, GTK_STATE_NORMAL, c);
	(c == NULL) ? NULL : (c = (g_free (c), NULL));
}


void document_header_on_close_btn_clicked (DocumentHeader* self) {
	g_return_if_fail (self != NULL);
	if (document_close (self->priv->doc)) {
		gtk_object_destroy ((GtkObject*) self->priv->doc);
	}
}


const char* document_header_get_text (DocumentHeader* self) {
	g_return_val_if_fail (self != NULL, NULL);
	return gtk_label_get_text (self->priv->title_lbl);
}


void document_header_set_text (DocumentHeader* self, const char* value) {
	g_return_if_fail (self != NULL);
	gtk_label_set_text (self->priv->title_lbl, value);
	g_object_notify ((GObject *) self, "text");
}


static GObject * document_header_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	DocumentHeaderClass * klass;
	GObjectClass * parent_class;
	DocumentHeader * self;
	klass = DOCUMENT_HEADER_CLASS (g_type_class_peek (TYPE_DOCUMENT_HEADER));
	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DOCUMENT_HEADER (obj);
	{
		GtkButton* _tmp1;
		GtkImage* _tmp0;
		GtkLabel* _tmp2;
		_tmp1 = NULL;
		_tmp0 = NULL;
		self->priv->close_btn = (_tmp1 = utils_create_small_button (_tmp0 = g_object_ref_sink ((GtkImage*) gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU))), (self->priv->close_btn == NULL) ? NULL : (self->priv->close_btn = (g_object_unref (self->priv->close_btn), NULL)), _tmp1);
		(_tmp0 == NULL) ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL));
		_tmp2 = NULL;
		self->priv->title_lbl = (_tmp2 = g_object_ref_sink ((GtkLabel*) gtk_label_new ("")), (self->priv->title_lbl == NULL) ? NULL : (self->priv->title_lbl = (g_object_unref (self->priv->title_lbl), NULL)), _tmp2);
		gtk_box_pack_start_defaults ((GtkBox*) self, (GtkWidget*) self->priv->title_lbl);
		gtk_box_pack_start_defaults ((GtkBox*) self, (GtkWidget*) self->priv->close_btn);
		gtk_widget_show_all ((GtkWidget*) self);
	}
	return obj;
}


static void document_header_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	DocumentHeader * self;
	gpointer boxed;
	self = DOCUMENT_HEADER (object);
	switch (property_id) {
		case DOCUMENT_HEADER_TEXT:
		g_value_set_string (value, document_header_get_text (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void document_header_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	DocumentHeader * self;
	self = DOCUMENT_HEADER (object);
	switch (property_id) {
		case DOCUMENT_HEADER_TEXT:
		document_header_set_text (self, g_value_get_string (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void document_header_class_init (DocumentHeaderClass * klass) {
	document_header_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DocumentHeaderPrivate));
	G_OBJECT_CLASS (klass)->get_property = document_header_get_property;
	G_OBJECT_CLASS (klass)->set_property = document_header_set_property;
	G_OBJECT_CLASS (klass)->constructor = document_header_constructor;
	G_OBJECT_CLASS (klass)->finalize = document_header_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_HEADER_TEXT, g_param_spec_string ("text", "text", "text", NULL, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void document_header_instance_init (DocumentHeader * self) {
	self->priv = DOCUMENT_HEADER_GET_PRIVATE (self);
	self->priv->doc = NULL;
}


static void document_header_finalize (GObject* obj) {
	DocumentHeader * self;
	self = DOCUMENT_HEADER (obj);
	(self->priv->close_btn == NULL) ? NULL : (self->priv->close_btn = (g_object_unref (self->priv->close_btn), NULL));
	(self->priv->title_lbl == NULL) ? NULL : (self->priv->title_lbl = (g_object_unref (self->priv->title_lbl), NULL));
	G_OBJECT_CLASS (document_header_parent_class)->finalize (obj);
}


GType document_header_get_type (void) {
	static GType document_header_type_id = 0;
	if (document_header_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (DocumentHeaderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) document_header_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DocumentHeader), 0, (GInstanceInitFunc) document_header_instance_init, NULL };
		document_header_type_id = g_type_register_static (GTK_TYPE_HBOX, "DocumentHeader", &g_define_type_info, 0);
	}
	return document_header_type_id;
}




